#include "StdAfx.h"
#include "GTeam.h"
#include "Role.h"
#include "Card.h"

GTeam g_team;
GTeam& GTeam::Inst()
{
	return g_team;
}
GTeam::GTeam(void)
{
	InitializeCriticalSection(&m_cs);
}

GTeam::~GTeam(void)
{
	DeleteCriticalSection(&m_cs);
}

GRole* GTeam::GetLeftMember()
{
	return m_pMember[0];
}

GRole* GTeam::GetRightMember()
{
	return m_pMember[2];
}

GRole* GTeam::GetLocalMember()
{
	return m_pMember[1];
}

void GTeam::SetLeftMember(GRole* pMember)
{
	m_pMember[0] = pMember;
}
void GTeam::SetLocalMember(GRole* pMember)
{
	m_pMember[1] = pMember;
}
void GTeam::SetRightMember(GRole* pMember)
{
	m_pMember[2] = pMember;
}

DWORD GTeam::GetTeamID()
{
	return m_dwTeamID;
}

void GTeam::RandomCard()
{
	 srand((unsigned)time( NULL));
	 int card[54] = {0};
	 int leftCard[17] = {0};
	 int rigthCard[17] = {0};
	 for (int i=0; i<54;i++) card[i] = i+1;

	 int index = 0;
	 for (int i=0; i<3; i++)
	 {	
		 index = rand()%(54-i);
		 m_ThreeCard[i] = card[index];
		 card[index] = card[54-1-i];
	 }

	 for (int i=0; i<17; i++)
	 {
		 index = rand()%(51-i);
		 leftCard[i] = card[index];
		 card[index] = card[51-1-i];
	 }

	 for (int i=0; i<17; i++)
	 {
		 index = rand()%(34-i);
		 rigthCard[i] = card[index];
		 card[index] = card[34-1-i];
	 }

	 EnterCriticalSection(&m_cs);
	 m_pMember[0]->AddCard(leftCard, 17);
	 m_pMember[1]->AddCard(card, 17);
	 m_pMember[2]->AddCard(rigthCard, 17);
	 LeaveCriticalSection(&m_cs);
}

void GTeam::Draw(int w, int h)
{
	if (GTeam::Inst().GetLeftMember() == NULL) return;
	EnterCriticalSection(&m_cs);
	m_pMember[0]->Draw(w,h);
	m_pMember[1]->DrawLeft(w,h);
	m_pMember[2]->DrawRight(w, h);
	GCard::Inst().Draw(m_ThreeCard[0], 370, h-200);
	GCard::Inst().Draw(m_ThreeCard[1], 470, h-200);
	GCard::Inst().Draw(m_ThreeCard[2], 570, h-200);
	LeaveCriticalSection(&m_cs);
}